package com.stms.service.impl;

import com.stms.dao.ContractRepository;
import com.stms.dao.DepotRepository;
import com.stms.dao.PurchaseRepository;
import com.stms.pojo.Purchase;
import com.stms.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Autowired
    private PurchaseRepository purchaseRepository;
    @Autowired
    private ContractRepository contractRepository;
    @Autowired
    private DepotRepository depotRepository;

    @Override
    public boolean addPurchase(Purchase purchase) {
        if (contractRepository.findByContractId(purchase.getContractId())==null||depotRepository.findByDepotId(purchase.getDepotId())==null) {
            return false;
        }
        if (purchaseRepository.save(purchase)!=null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean editPurchase(Purchase purchase) {
        if (contractRepository.findByContractId(purchase.getContractId())==null||depotRepository.findByDepotId(purchase.getDepotId())==null) {
            return false;
        }
        if (purchaseRepository.save(purchase)!=null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void deletaBatch(String purchaseIds) {
        String[] purchaseStr = purchaseIds.split(",");
        List<Integer> list = new ArrayList<>();

        for(String str:purchaseStr){
            list.add(Integer.valueOf(str));
        }
        purchaseRepository.deleteBatch(list);
    }

    @Override
    public Page<Purchase> fuzzyQuery(Integer pageNum,Integer pageSize,String direction,Purchase purchase) {
        if (pageNum==null||pageNum<=0){
            pageNum=1;
        }
        pageNum = pageNum-1;
        if (pageSize==null){
            pageNum=10;
        }
        if (direction==null){
            direction="ASC";
        }

        Date startDate = purchase.getStartDate();
        Date endDate = purchase.getEndDate();

        Sort.Direction direc = direction.equals("ASC")?Sort.Direction.ASC:Sort.Direction.DESC;
        Pageable pageable = PageRequest.of(pageNum,pageSize,direc,"id");

        Page<Purchase> page = purchaseRepository.findAll(new Specification<Purchase>(){
            @Override
            public Predicate toPredicate(Root<Purchase> root, CriteriaQuery<?> query, CriteriaBuilder cb){
                List<Predicate> list = new ArrayList<>();
                if (purchase==null){
                    return null;
                }
                if (purchase.getId()!=null&&!purchase.getId().equals("")){
                    list.add(cb.equal(root.get("id"),purchase.getId()));
                }
                if (purchase.getContractId()!=null&&!purchase.getContractId().equals("")){
                    list.add(cb.equal(root.get("contractId"),purchase.getContractId()));
                }
                if (purchase.getDepotId()!=null&&!purchase.getDepotId().equals("")){
                    list.add(cb.equal(root.get("depotId"),purchase.getDepotId()));
                }
                if (startDate!=null&&!startDate.equals("")){
                    list.add(cb.greaterThanOrEqualTo(root.get("writeDate"),startDate));
                }
                if (endDate!=null&&!endDate.equals("")){
                    list.add(cb.lessThanOrEqualTo(root.get("writeDate"),endDate));
                }

                Predicate[] predicate = new Predicate[list.size()];
                return cb.and(list.toArray(predicate));
            }
        },pageable);
            return page;
        }

    @Override
    public Page<Purchase> findByPage(Integer pageNum, Integer pageSize) {
        if (pageNum==null||pageNum==0){
            pageNum=1;
        }
        pageNum = pageNum-1;
        if (pageSize==null){
            pageSize=10;
        }

        PageRequest of = PageRequest.of(pageNum,pageSize);
        Page<Purchase> page = purchaseRepository.findAll(of);
        return page;
    }
}
